/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poo.biblioteca.vistas.prestamos;

import biblioteca.entidades.Ejemplar;
import biblioteca.entidades.Lector;
import biblioteca.entidades.Libro;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author apo
 */
public class PnlRegistrarPrestamo extends javax.swing.JPanel {

    /**
     * Creates new form PnlRegistrarPrestamo
     */
    public PnlRegistrarPrestamo() {
        initComponents();
    }
    private Lector lectorSeleccionado;
    private Libro libroSeleccionado;
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtIdPrestamo = new javax.swing.JTextField();
        lblIdPrestamo = new javax.swing.JLabel();
        lblFechaPrestamo = new javax.swing.JLabel();
        lblFechaProgramadaDevolucion = new javax.swing.JLabel();
        lblFechaDevolucion = new javax.swing.JLabel();
        txtLector = new javax.swing.JTextField();
        btnBuscarLibro = new javax.swing.JButton();
        txtLibro = new javax.swing.JTextField();
        cmbEjemplar = new javax.swing.JComboBox();
        btnBuscarLector = new javax.swing.JButton();
        lblLector = new javax.swing.JLabel();
        lblTituloLibro = new javax.swing.JLabel();
        lblEjemplar = new javax.swing.JLabel();
        jdcFechaDePrestamo = new com.toedter.calendar.JDateChooser();
        jdcFechaProgramadaDevolucion = new com.toedter.calendar.JDateChooser();
        jdcFechaDevolucion = new com.toedter.calendar.JDateChooser();
        lblMensaje = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Préstamo"));

        txtIdPrestamo.setEnabled(false);

        lblIdPrestamo.setText("Id Préstamo:");

        lblFechaPrestamo.setText("Fecha de Préstamo:");

        lblFechaProgramadaDevolucion.setText("Fecha Programada de Devolución:");

        lblFechaDevolucion.setText("Fecha Efectiva de Devolución:");

        txtLector.setEnabled(false);

        btnBuscarLibro.setText("...");

        txtLibro.setEnabled(false);

        btnBuscarLector.setText("...");

        lblLector.setText("Lector:");

        lblTituloLibro.setText("Título libro:");

        lblEjemplar.setText("Ejemplar a prestar:");

        jdcFechaProgramadaDevolucion.setEnabled(false);

        jdcFechaDevolucion.setEnabled(false);

        lblMensaje.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblIdPrestamo)
                    .addComponent(lblFechaPrestamo)
                    .addComponent(lblFechaProgramadaDevolucion)
                    .addComponent(lblFechaDevolucion)
                    .addComponent(lblLector)
                    .addComponent(lblTituloLibro)
                    .addComponent(lblEjemplar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jdcFechaDevolucion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jdcFechaProgramadaDevolucion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jdcFechaDePrestamo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtLibro, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtLector))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnBuscarLector, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnBuscarLibro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtIdPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 260, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbEjemplar, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblIdPrestamo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFechaPrestamo)
                    .addComponent(jdcFechaDePrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblFechaProgramadaDevolucion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblFechaDevolucion))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jdcFechaProgramadaDevolucion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jdcFechaDevolucion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLector)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtLector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnBuscarLector)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLibro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTituloLibro)
                    .addComponent(btnBuscarLibro))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmbEjemplar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblEjemplar)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarLector;
    private javax.swing.JButton btnBuscarLibro;
    private javax.swing.JComboBox cmbEjemplar;
    private com.toedter.calendar.JDateChooser jdcFechaDePrestamo;
    private com.toedter.calendar.JDateChooser jdcFechaDevolucion;
    private com.toedter.calendar.JDateChooser jdcFechaProgramadaDevolucion;
    private javax.swing.JLabel lblEjemplar;
    private javax.swing.JLabel lblFechaDevolucion;
    private javax.swing.JLabel lblFechaPrestamo;
    private javax.swing.JLabel lblFechaProgramadaDevolucion;
    private javax.swing.JLabel lblIdPrestamo;
    private javax.swing.JLabel lblLector;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JLabel lblTituloLibro;
    private javax.swing.JTextField txtIdPrestamo;
    private javax.swing.JTextField txtLector;
    private javax.swing.JTextField txtLibro;
    // End of variables declaration//GEN-END:variables

    //IdPrestamo
    public void setIdPrestamo(String idPrestamo) {
        txtIdPrestamo.setText(idPrestamo);
    }

    public String getIdPrestamo() {
        return txtIdPrestamo.getText();
    }

    //FechaPrestamo
    public void setFechaPrestamo(Date fechaPrestamo) {
        jdcFechaDePrestamo.setDate(fechaPrestamo);
    }

    public Date getFechaPrestamo() {
        return jdcFechaDePrestamo.getDate();
    }

    //FechaProgramadaDevolucion
    public void setFechaProgramadaDevolucion(Date fechaProgramadaDevolucion) {
        jdcFechaProgramadaDevolucion.setDate(fechaProgramadaDevolucion);
    }

    public Date getFechaProgramadaDevolucion() {
        return jdcFechaProgramadaDevolucion.getDate();
    }

    //FechaEfectivaDevolucion
    public void setFechaEfectivaDevolucion(Date fechaEfectivaDevolucion) {
        jdcFechaDevolucion.setDate(fechaEfectivaDevolucion);
    }

    public Date getFechaEfectivaDevolucion() {
        return jdcFechaDevolucion.getDate();
    }

    //Ejemplar
    public void setEjemplares(ArrayList<Ejemplar> ejemplares) {
        cmbEjemplar.removeAllItems();

        if (ejemplares.size() == 0) {
            lblMensaje.setText("No hay ejemplares disponibles");
            return;
        } else {
            lblMensaje.setText("");
            for (Ejemplar e : ejemplares) {
                cmbEjemplar.addItem(e);
            }
        }

    }

    public void setEjemplar(Ejemplar ejemplar) {
        for (int i = 0; i < cmbEjemplar.getItemCount(); i++) {
            if (ejemplar.getIdEjemplar() == ((Ejemplar) cmbEjemplar.getItemAt(i)).getIdEjemplar()) {
                cmbEjemplar.setSelectedIndex(i);
                return;
            }
        }
    }

    public Ejemplar getEjemplar() {
        return (Ejemplar) cmbEjemplar.getSelectedItem();
    }
    //Lector

    public void btnBuscarLectorAddActionListener(ActionListener actionListener) {
        btnBuscarLector.addActionListener(actionListener);
    }

    public void setLector(Lector lector) {
        txtLector.setText(lector.toString());
        lectorSeleccionado = lector;

    }

    public Lector getLector() {
        return lectorSeleccionado;
    }

    public void btnBuscarLibroAddActionListener(ActionListener actionListener) {
        btnBuscarLibro.addActionListener(actionListener);
    }

    public void setLibro(Libro libroSeleccionado) {
        txtLibro.setText(libroSeleccionado.toString());
        this.libroSeleccionado = libroSeleccionado;
    }

    public Libro getLibro() {
        return libroSeleccionado;
    }

    void setFechaPrestamoEnabled(boolean b) {
        jdcFechaDePrestamo.setEnabled(b);
    }
}
